<template>
  <div class="login">
    <div class="container">
      <div class="login-container">
        <div class="card">
          <div class="login-header">
            <h1>用户登录</h1>
            <p>欢迎回来！请登录您的账号</p>
          </div>

          <form @submit.prevent="handleLogin" class="login-form">
            <div class="form-group">
              <label class="form-label">用户名</label>
              <input
                v-model="formData.username"
                type="text"
                class="form-input"
                placeholder="请输入用户名"
                :disabled="authStore.isLoading.value"
                required
              />
            </div>

            <div class="form-group">
              <label class="form-label">密码</label>
              <input
                v-model="formData.password"
                type="password"
                class="form-input"
                placeholder="请输入密码"
                :disabled="authStore.isLoading.value"
                required
              />
            </div>

            <div v-if="authStore.error.value" class="error-message">
              {{ authStore.error.value }}
            </div>

            <button
              type="submit"
              class="btn login-btn"
              :disabled="authStore.isLoading.value || !isFormValid"
              :class="{ loading: authStore.isLoading.value }"
            >
              {{ authStore.isLoading.value ? '登录中...' : '登录' }}
            </button>

            <div class="login-footer">
              <p>还没有账号？</p>
              <router-link to="/register" class="link">立即注册</router-link>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'

const router = useRouter()
const authStore = useAuthStore()

// 表单数据
const formData = reactive({
  username: 'testuser', // 默认测试账号
  password: '123456',   // 默认测试密码
})

// 表单验证
const isFormValid = computed(() => {
  return formData.username.length >= 3 && formData.password.length >= 6
})

// 处理登录
async function handleLogin() {
  if (!isFormValid.value) {
    return
  }

  try {
    await authStore.login(formData)
    
    // 登录成功，跳转到首页
    await router.push('/')
  } catch (error) {
    console.error('Login error:', error)
  }
}

// 页面加载时清除错误
onMounted(() => {
  authStore.clearError()
})
</script>

<style scoped>
.login {
  min-height: calc(100vh - 140px);
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.login-container {
  width: 100%;
  max-width: 400px;
}

.login-header {
  text-align: center;
  margin-bottom: 2rem;
}

.login-header h1 {
  font-size: 2rem;
  font-weight: bold;
  color: #333;
  margin-bottom: 0.5rem;
}

.login-header p {
  color: #666;
  font-size: 0.9rem;
}

.login-form {
  width: 100%;
}

.login-btn {
  width: 100%;
  margin-bottom: 1.5rem;
}

.login-footer {
  text-align: center;
  color: #666;
  font-size: 0.9rem;
}

.link {
  color: #667eea;
  text-decoration: none;
  font-weight: 500;
  margin-left: 0.5rem;
}

.link:hover {
  text-decoration: underline;
}

@media (max-width: 480px) {
  .login-container {
    margin: 0 1rem;
  }
  
  .card {
    padding: 2rem 1.5rem;
  }
}
</style>
